package com.zengqingfa.rabbitmq.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.UUID;

import static com.zengqingfa.rabbitmq.config.DelayedRabbitMQConfig.DELAYED_EXCHANGE_NAME;
import static com.zengqingfa.rabbitmq.config.DelayedRabbitMQConfig.DELAYED_ROUTING_KEY;
import static com.zengqingfa.rabbitmq.config.RabbitMqConfig.*;

/**
 * @author zengqingfa
 * @className RabbitmqController
 * @description
 * @create 2022/9/25 9:44
 */
@Slf4j
@RestController
@RequestMapping("/rabbitmq")
public class RabbitmqController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/sendShortMsg")
    public boolean sendShortMsg(@RequestParam("msg") String msg) {
        log.info("sendShortMsg 队列A当前时间：{}", new Date().toString());
        MessageProperties messageProperties = new MessageProperties();
        //设置过期时间
        messageProperties.setExpiration("2000");
        //这个参数是用来做消息的唯一标识
        //发布消息时使用，存储在消息的headers中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        Message message = new Message(JSON.toJSONString(msg).getBytes(StandardCharsets.UTF_8), messageProperties);
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE_NAME, DELAY_QUEUEC_ROUTING_KEY, message, correlationData);
        return true;
    }

    @GetMapping(value = "/sendLongMsg")
    public boolean sendLongMsg(@RequestParam("msg") String msg) {
        log.info("sendLongMsg 队列B当前时间：{}", new Date().toString());
        MessageProperties messageProperties = new MessageProperties();
        //设置过期时间
        messageProperties.setExpiration("20000");
        //这个参数是用来做消息的唯一标识
        //发布消息时使用，存储在消息的headers中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        Message message = new Message(JSON.toJSONString(msg).getBytes(StandardCharsets.UTF_8), messageProperties);
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE_NAME, DELAY_QUEUEC_ROUTING_KEY, message, correlationData);
        return true;
    }



    @RequestMapping("/delayMsg")
    public void delayMsg(String msg, Integer delayTime) {
        log.info("当前时间：{},收到请求，msg:{},delayTime:{}", new Date(), msg, delayTime);
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, msg, a ->{
            //发送消息的时候设置延迟时间
            a.getMessageProperties().setDelay(delayTime);
            return a;
        });
    }
}